package com.ruijie.acl.importdevice.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ruijie.emp.asynchandle.AsyncHandleInstance;
import com.ruijie.emp.asynchandle.AsyncHandleLog;
import com.ruijie.emp.asynchandle.AsyncHandleService;
import com.ruijie.emp.asynchandle.AsyncService;
import com.ruijie.foundation.server.ServerStatus;

/**
 * <p>
 * Title: Module Information
 * </p>
 * <p>
 * Description: 调用handle,实现异步
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time: 2009-07-14
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 *@author ASAI
 */
public class ImportDeviceAsyncServiceImpl implements AsyncService {

	protected static final Log logger = LogFactory.getLog(ImportDeviceAsyncServiceImpl.class);
	
	private AsyncHandleService asyncHandleService ;
	
	public AsyncHandleService getAsyncHandleService() {
		return asyncHandleService;
	}

	public void setAsyncHandleService(AsyncHandleService asyncHandleService) {
		this.asyncHandleService = asyncHandleService;
	}

	@Override
	public AsyncHandleInstance getInstance() {
		return asyncHandleService.getInstance() ;
	}

	@Override
	public List<AsyncHandleLog> getLogger(int rowNum) {
		return asyncHandleService.getLogger(rowNum);
	}

	@Override
	public ServerStatus getStatus() {
		return asyncHandleService.getStatus() ;
	}

	@Override
	public boolean isRunning() {
		return asyncHandleService.isRunning() ;
	}

	@Override
	public void start(final Object object) {
		if( !asyncHandleService.validateHandle(object) ) return ;
        Thread t = new Thread() {
            @Override
            public void run() {
                try{
                    logger.info("start aysnc handle service");
                    asyncHandleService.asyncHandle(object) ;
                    logger.info("end aysnc handle service");
                }catch(Throwable ex){
                    logger.info("error async handle servce", ex);
                }
            }
        };
        t.start();  
	}

	@Override
	public void stop() {
		asyncHandleService.stop() ;
	}

}

